package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.BaseObservable;
import android.support.annotation.NonNull;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;

/**
 * Created by zxh on 16/11/17.
 */

public class ContextObservable extends BaseObservable {
    private Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public ContextObservable(@NonNull Context context) {
        this.context = context;
    }

    public void runOnUiThread(Runnable runnable) {
        ((Activity) getContext()).runOnUiThread(runnable);
    }

    public void showToastLong(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show();
            }
        });
    }

    public void showToast(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void notifyDataSetChanged(final ArrayAdapter adapter) {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    public void notifyDataSetChanged(final XListAdapterInterface adapter) {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }
}
